<?php
namespace app\astro\controller;
use \app\check\controller\Check;
use app\common\model\Recode;

class Davison extends Check
{
    /**
     * @return \think\response\Json
     * @throws \app\common\lib\exception\ApiException
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function get()
    {
        $Data = new Data();
        $Set = new \app\user\controller\Astroset();
        $astro_set = $Set->get(true,'davison');// 内部调用
        $Data->planets_display = $astro_set['value']['planets_display'];
        $Data->asps_display = $astro_set['value']['asps_display'];
        $Data->asp_orb_all = $astro_set['value']['asp_orb_all'];
        $Data->planet_orb_all = $astro_set['value']['planet_orb_all'];

        $map = ['id' => input('id1/d', 0), 'uid' => $this->user['id']];

        $RecodeModel = new Recode();

        $userinfo1 = $RecodeModel->getRecode($map);

        $map['id'] = input('id2/d', 0);
        $userinfo2 = $RecodeModel->getRecode($map);

        $timestamp1 = $Data->deal_time($userinfo1);
        $timestamp2 = $Data->deal_time($userinfo2);

        $longitude1 = ($userinfo1['long_deg'] + $userinfo1['long_min'] / 60 ) * $userinfo1['ew'];
        $latitude1 = ($userinfo1['lat_deg'] + $userinfo1['lat_min'] / 60 ) * $userinfo1['ns'];
        $longitude2 = ($userinfo2['long_deg'] + $userinfo2['long_min'] / 60 ) * $userinfo2['ew'];
        $latitude2 = ($userinfo2['lat_deg'] + $userinfo2['lat_min'] / 60 ) * $userinfo2['ns'];

        $timestamp = ($timestamp1 + $timestamp2) /2 ;
        $utdatenow = strftime("%d.%m.%Y", $timestamp);
        $utnow = strftime("%H:%M:%S", $timestamp);
        $longitude = ($longitude1 + $longitude2) / 2;
        $latitude = ($latitude1 + $latitude2) / 2;

        $options = [
            'utdatenow' => $utdatenow,
            'utnow' => $utnow,
            'longitude' => $longitude,
            'latitude' => $latitude,
            'h_sys' => $astro_set['value']['h_sys'],
        ];

        $out = $Data->execData($options);

        $original_data = $Data->getOriginalData($out,$astro_set['value']['h_sys']);
        $Data->array_remove($original_data,$Data->planets_display);
        $details = $Data->getPlanetHouseDetail($original_data, $original_data['hc1']);
        $phase = $Data->planetPkPlanet($original_data, $original_data,true);
        foreach ($phase['planets'] as $k => $d)
        {
            $details['planets'][$k]['phase'] = $d['phase'];
            if(empty($d['phase'])) $details['planets'][$k]['phase'] = null;
        }
        $details['phase'] = $phase['phase'];

        $details['planets'] = (object)$details['planets'];// 指定输出为object  防止所有行星全开时 类型改变

        return json(['code'=>200,'data'=>$details,'msg'=>'Success.']);
    }
}